#include "namespace.h"

namespace byhh_variables{
    /// NEVER USE THE URL!! IT WILL WASTE DAMN LOTS OF TIME!!
    //const QUrl server("http://byhh.net/");
    const QUrl server("http://202.114.0.248/");
    
    const QString queryViewFavorite 	= "/cgi-bin/bbsbrd";
    const QString queryAllBoard 	= "/cgi-bin/bbsmybrd";
    const QString queryToLogin 		= "/cgi-bin/bbslogin?id=%1&pw=%2";
    const QString queryToLogout 	= "/cgi-bin/bbslogout";
    const QString queryViewBBS 		= "/cgi-bin/bbsboa?%1";
    const QString queryViewBoard 	= "/cgi-bin/bbstdoc?board=%1";
    const QString queryViewTopic 	= "/cgi-bin/bbstcon?board=%1&file=%2";
    const QString sendNewPost 		= "/cgi-bin/bbssnd?board=%1";
    const QString sendContent 		= "title=%1&signature=%2&start=0&text=%3";
    
    int _count = 0;
    //保存正在处理中的http连接
    QMap <int , QHttp *> httpMaps;
    //记录已经废弃的http连接，统一删除
    QList<QHttp *> toDelete;
    //byhh.net返回的页面编码是GBK
    QTextCodec * gbkcodec = QTextCodec::codecForName("GBK");
    
    int request (QHttp * http, Profile * profile, QString url, QString type , QString text)    {
	qDebug() << ">>" << type<< url ;
	QString cookies = profile->getCookies();
	QHttpRequestHeader header(type, url);
	header.setValue("Host", server.host());
	header.setValue("Accept", "*/*");
	header.setValue("Cookie", cookies);
	header.setValue("Connection", "keep-alive");
	header.setContentType("*/*");
	header.setContentLength(text.length());
	http-> setHost(server.host());
	return http-> request(header, gbkcodec->fromUnicode(text));
    }

    // If the caller dose NOT provide a 'http', thus we search from httpMaps, and then delete it.
    QString fetchHtml(int num , bool error, QHttp * http){
	QByteArray response;
	QString html = "";
	bool drop = false;
	if (http == 0){
	    drop = true;
	    http = httpMaps.value(num , 0);
	    httpMaps.remove(num);
	    if (!http) return html;
	}
	if (error) return html;
	response = http -> readAll();
	html = gbkcodec->toUnicode( response);
	if (drop) toDelete << http;
	return html;
    }

//     QString pureString(QString str){
// 	QRegExp exp("\033\[(\\d*);?(\\d*)m");
// 	int pos = 0;
// 	    str.remove(exp);
// 	//while(( pos = exp.IndexOf(str, pos)) != -1){
// 	//}
//     }

}

